package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import com.itheima.service.MenuService;
import org.apache.dubbo.config.annotation.Service;
import org.bouncycastle.asn1.cms.PasswordRecipientInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Program: health_parent
 * @Description:
 * @Author: choulan
 * @Date:2020-11-27 16:26
 **/
@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuDao;
    @Autowired
    private RoleDao roleDao;
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //获取到分页参数
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //使用分页插件
        PageHelper.startPage(currentPage,pageSize);
        //将查询到的数据封装到Page对象中
        Page<Menu> page= menuDao.selectByCondition(queryString);
        long total = page.getTotal();
        //得到了所有的菜单信息
        List<Menu> rows = page.getResult();
        //查找每个菜单的子菜单
        for (int i = 0; i < rows.size(); i++) {
            Integer menuId = rows.get(i).getId();
            //通过父菜单id查找对应的所有子菜单
            List<Menu> childrenMenu = menuDao.findChildrenByPrentMenuId(menuId);
            if(childrenMenu!=null&&childrenMenu.size()>0){//当前父菜单有子菜单
                //5.将子菜单信息加入到父菜单信息中去
                rows.get(i).setChildren(childrenMenu);
            }
        }
        //判断一下menus中的menu是否有子菜单且level不为1，没有就删除
        for (int i = 0; i < rows.size(); i++) {
            List<Menu> children = rows.get(i).getChildren();
            if(children.size()==0&&rows.get(i).getLevel()!=1){
                rows.remove(i);
                i--;
            }
        }
        return new PageResult(total,rows);
    }

    @Override
    public List<Menu> findParentMenuIds() {
        List<Menu> menus = menuDao.findMenu();
        if(menus==null){
            return null;
        }
        return menus;
    }

    @Override
    public void delete(Integer id) {
        //id是menuId
        //清除role-menu的关联关系
        menuDao.deleteRoleMenuAssociationByMenuId(id);
        //删除该菜单
        menuDao.delete(id);

    }


    @Override
    public void add(Menu menu) {
        //menu中现在有name,linkUrl,level,parentMenuId,icon,description,还差path,priority
        //设置path和priority

        if(menu.getParentMenuId()==null||"".equals(menu.getParentMenuId())){
            //当前新增菜单为一级菜单
            //查找所有的一级菜单的path和priority
            String tempPath=String.valueOf(Integer.parseInt(menuDao.findMaxPath())+1) ;
            Integer maxPriority= menuDao.findMaxPriority();
            menu.setPath(tempPath);
            menu.setPriority(maxPriority+1);
        }else {
            //当前菜单不为一级菜单
            //查找当前菜单的父菜单有多少子菜单
            Integer parentMenuId = menu.getParentMenuId();
            Integer count= menuDao.findCountByParentMenuId(parentMenuId);
            //查找父菜单的path
            String path=menuDao.findPathById(parentMenuId);
            String newPath="/"+path+"-"+(count+1);
            menu.setPath(newPath);
            //设置priority
            menu.setPriority(count+1);
        }
        menuDao.add(menu);

    }

    @Override
    public void edit(Menu menu) {
        //设置priority
        Integer parentMenuId = menu.getId();
        Integer count= menuDao.findCountByParentMenuId(parentMenuId);
        menu.setPriority(count+1);
        menuDao.edit(menu);
    }

    @Override
    public Menu findById(Integer id) {
        return menuDao.findByMenuId(id);
    }

    @Override
    public String findParentMenuId(Integer childrenId) {
        Integer id = menuDao.findParentMenuName(childrenId);
        return String.valueOf(id);
    }

    @Override
    public Menu findMenuById(Integer id) {
        return menuDao.findMenuById(id);
    }

    @Override
    public boolean checkName(String name) {
        Integer i= menuDao.checkName(name);
        if(i==0){
            return false;
        }
        return true;
    }


}
